package com.boxedinsoftware.betamax.sms;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class AccountsSQLHelper {

   private static final String DATABASE_NAME = "bmaxsmsacc.db";
   private static final int DATABASE_VERSION = 1;
   private static final String TABLE_NAME = "accounts";
   //private static final String RULES_TABLE_NAME = "rules";

   private Context context;
   private SQLiteDatabase db;

   private SQLiteStatement insertStmt;
   private static final String INSERT = "insert into " 
      + TABLE_NAME + "(provider, username , password, balance) values (?,?,?,?)";
   
   //private SQLiteStatement insertRulesStmt;
   //private static final String INSERT_RULES = "insert into " 
      //+ RULES_TABLE_NAME + "(object, choice) values (?,?)";
   
   private SQLiteStatement updateStmt;
   private static final String UPDATE = "update " 
      + TABLE_NAME + " SET balance = ? WHERE id = ?";
   
   private SQLiteStatement updateAccStmt;
   private static final String UPDATE_ACC = "update " 
      + TABLE_NAME + " SET provider = ? , username = ? , password = ? , balance = ? WHERE id = ?";

   public AccountsSQLHelper(Context context) {
      this.context = context;
      OpenHelper openHelper = new OpenHelper(this.context);
      this.db = openHelper.getWritableDatabase();
      this.insertStmt = this.db.compileStatement(INSERT);
      this.updateStmt = this.db.compileStatement(UPDATE);
      this.updateAccStmt = this.db.compileStatement(UPDATE_ACC);
      //this.insertRulesStmt = this.db.compileStatement(INSERT_RULES);
   }

   public long insert(String provider, String username, String password, Double balance) {
      this.insertStmt.bindString(1, provider);
      this.insertStmt.bindString(2, username);
      this.insertStmt.bindString(3, password);
      this.insertStmt.bindDouble(4, balance);
      return this.insertStmt.executeInsert();
   }
   
   /*public long insertRule(String rule , int choice) {
	      this.insertRulesStmt.bindString(1, rule);
	      this.insertRulesStmt.bindLong(2, choice);
	      return this.insertRulesStmt.executeInsert();
	   }*/
   
   public void update(double balance, int id) {
	      this.updateStmt.bindDouble(1, balance);
	      this.updateStmt.bindLong(2, id);
	      this.updateStmt.execute();
	   }
   
   public void update(double balance, int id , String provider , String username , String password ) {
	      this.updateAccStmt.bindString(1, provider);
	      this.updateAccStmt.bindString(2, username);
	      this.updateAccStmt.bindString(3, password);
	      this.updateAccStmt.bindDouble(4, balance);
	      this.updateAccStmt.bindLong(5, id);
	      this.updateAccStmt.execute();
	   }

   public void deleteAll() {
      this.db.delete(TABLE_NAME, null, null);
   }
   
   /*public void deleteAllRules() {
	      this.db.delete(RULES_TABLE_NAME, null, null);
	   }*/
   
   public void deleteRow(int ID) {
	      this.db.delete(TABLE_NAME, "id=" + ID, null);
	   }
   
   /*public void deleteRowRules(int ID) {
	      this.db.delete(RULES_TABLE_NAME, "id=" + ID, null);
	   }*/

   public Cursor selectAll() {
      Cursor cursor = this.db.query(TABLE_NAME, new String[] {"id", "provider" , "username" , "balance" }, 
        null, null, null, null, "provider asc");
      return cursor;
   }
   
   /*public Cursor selectAllRules() {
	      Cursor cursor = this.db.query(RULES_TABLE_NAME, new String[] {"id", "object" , "choice" }, 
	        null, null, null, null, "id asc");
	      return cursor;
	   }*/
   
   public int getCount() {
	      Cursor cursor = this.db.query(TABLE_NAME, new String[] {"id"}, 
	        null, null, null, null, null);
	      return cursor.getCount();
	   }
   
   public Cursor selectAllForBalance() {
	      Cursor cursor = this.db.query(TABLE_NAME, new String[] {"id", "provider" , "username" , "password", "balance" }, 
	        null, null, null, null, "provider asc");
	      return cursor;
	   }

   private static class OpenHelper extends SQLiteOpenHelper {

      OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, provider TEXT , username TEXT, password TEXT , access_number TEXT , balance DOUBLE)");
         //db.execSQL("CREATE TABLE " + RULES_TABLE_NAME + " (id INTEGER PRIMARY KEY, object TEXT , choice INTEGER)");
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         //db.execSQL("DROP TABLE IF EXISTS " + RULES_TABLE_NAME);
         onCreate(db);
      }
      
   }
   public void stop(){
	   
	   this.db.close();
	   
   }
}
